package com.cyxu.moviedigest.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import com.cyxu.moviedigest.adapters.MyAdapter;
import com.cyxu.moviedigest.R;
import com.cyxu.moviedigest.data.MovieInfo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {

    private String username = null;
    private String password = null;
    private JSONArray jsonArray;
    private List<MovieInfo> movies = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", MODE_PRIVATE);
        username = sharedPreferences.getString("username", null);
        password = sharedPreferences.getString("password", null);
        try {
            jsonArray = new JSONArray(sharedPreferences.getString("movie", "NULL"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (username == null) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
            return ;
        }

        setContentView(R.layout.activity_main);
        loadMovieList();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if(id == R.id.action_logout)  {
            SharedPreferences sharedPreferences = getSharedPreferences("userInfo", MODE_PRIVATE);
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString("username", null);
            edit.putString("password", null);
            edit.apply();
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void loadMovieList() {
        RecyclerView recyclerView;

        recyclerView = (RecyclerView)findViewById(R.id.main_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        movies.clear();
        getMovieRes();

        recyclerView.setAdapter(new MyAdapter(this, movies));
    }

    private void getMovieRes() {
        for(int i = 0; i < jsonArray.length(); ++i) {
            try {
                JSONObject jo = jsonArray.getJSONObject(i);
                MovieInfo movie = new MovieInfo();
                movie.setId(jo.getInt("id"));
                movie.setTitle(jo.getString("title"));
                movie.setRate(jo.getDouble("rating"));
                movie.setDescription(jo.getString("description"));
                movie.setDuration(jo.getInt("duration"));
                movie.setYear(jo.getInt("year"));
                movie.setGenres(jo.getJSONArray("genres"));
                movie.setDirectors(jo.getJSONArray("directors"));
                movie.setCasts(jo.getJSONArray("casts"));
                movie.setImage(jo.getJSONObject("images").getString("normal"));

                movies.add(movie);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}



